{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a0168a45-465f-4164-9a0b-fce7e5fe64ea",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-29T09:25:09.042595Z",
     "iopub.status.busy": "2022-05-29T09:25:09.042026Z",
     "iopub.status.idle": "2022-05-29T09:25:21.009725Z",
     "shell.execute_reply": "2022-05-29T09:25:21.008857Z",
     "shell.execute_reply.started": "2022-05-29T09:25:09.042520Z"
    }
   },
   "outputs": [],
   "source": [
    "# 导入包\n",
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8fcc3db0-4f1b-4285-8088-3476335810e9",
   "metadata": {},
   "source": [
    "## 一、重塑层次化索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "5223c205-ed73-44ec-9295-f211a82985a7",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-29T09:31:18.631089Z",
     "iopub.status.busy": "2022-05-29T09:31:18.630634Z",
     "iopub.status.idle": "2022-05-29T09:31:18.641928Z",
     "shell.execute_reply": "2022-05-29T09:31:18.641053Z",
     "shell.execute_reply.started": "2022-05-29T09:31:18.631063Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>科目</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>学生</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>小红</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>小蓝</th>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "科目  语文  数学  英语\n",
       "学生            \n",
       "小红   0   1   2\n",
       "小蓝   3   4   5"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建实验数据\n",
    "data = pd.DataFrame(\n",
    "    np.arange(6).reshape(2, 3),\n",
    "    index=pd.Index([\"小红\", \"小蓝\"], name=\"学生\"),\n",
    "    columns=pd.Index([\"语文\", \"数学\", \"英语\"], name=\"科目\"),\n",
    ")\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e93f0b6d-87d0-46e1-a10c-f40eb92e7895",
   "metadata": {},
   "source": [
    "### 1.1 stack函数：DataFrame --> Series(层级索引)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "e1d53b05-4c9b-466f-bd2b-ac92f82ebf5f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-29T09:57:40.509511Z",
     "iopub.status.busy": "2022-05-29T09:57:40.509181Z",
     "iopub.status.idle": "2022-05-29T09:57:40.516646Z",
     "shell.execute_reply": "2022-05-29T09:57:40.515497Z",
     "shell.execute_reply.started": "2022-05-29T09:57:40.509487Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "学生  科目\n",
       "小红  语文    0\n",
       "    数学    1\n",
       "    英语    2\n",
       "小蓝  语文    3\n",
       "    数学    4\n",
       "    英语    5\n",
       "dtype: int64"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# DataFrame转Series(层级索引)\n",
    "stackeData = data.stack()\n",
    "stackeData"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "247397a3-9488-42ec-bb4c-a511096622c2",
   "metadata": {},
   "source": [
    "### 1.2 unstack函数：Series(层级索引) --> DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "574f556a-f6b1-48cf-97a1-8adccf651cdc",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-29T09:57:53.382236Z",
     "iopub.status.busy": "2022-05-29T09:57:53.381799Z",
     "iopub.status.idle": "2022-05-29T09:57:53.391520Z",
     "shell.execute_reply": "2022-05-29T09:57:53.390728Z",
     "shell.execute_reply.started": "2022-05-29T09:57:53.382210Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>科目</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>学生</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>小红</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>小蓝</th>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "科目  语文  数学  英语\n",
       "学生            \n",
       "小红   0   1   2\n",
       "小蓝   3   4   5"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Series(层级索引)转DataFrame\n",
    "# 默认操作内层索引\n",
    "stackeData.unstack()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "38ebf799-23f4-4387-94c9-269fac60faed",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-29T09:58:02.819618Z",
     "iopub.status.busy": "2022-05-29T09:58:02.818774Z",
     "iopub.status.idle": "2022-05-29T09:58:02.828371Z",
     "shell.execute_reply": "2022-05-29T09:58:02.827153Z",
     "shell.execute_reply.started": "2022-05-29T09:58:02.819589Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>学生</th>\n",
       "      <th>小红</th>\n",
       "      <th>小蓝</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>科目</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>语文</th>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>数学</th>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>英语</th>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "学生  小红  小蓝\n",
       "科目        \n",
       "语文   0   3\n",
       "数学   1   4\n",
       "英语   2   5"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Series(层级索引)转DataFrame,\n",
    "# 指定操作外层索引，用层级编号\n",
    "stackeData.unstack(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "7311c7cd-f259-42dd-adb0-6e113f328bfa",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-29T09:58:12.712938Z",
     "iopub.status.busy": "2022-05-29T09:58:12.712380Z",
     "iopub.status.idle": "2022-05-29T09:58:12.722924Z",
     "shell.execute_reply": "2022-05-29T09:58:12.721884Z",
     "shell.execute_reply.started": "2022-05-29T09:58:12.712913Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>学生</th>\n",
       "      <th>小红</th>\n",
       "      <th>小蓝</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>科目</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>语文</th>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>数学</th>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>英语</th>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "学生  小红  小蓝\n",
       "科目        \n",
       "语文   0   3\n",
       "数学   1   4\n",
       "英语   2   5"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Series(层级索引)转DataFrame\n",
    "# 指定操作外层索引，用层级名\n",
    "stackeData.unstack(\"学生\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "2dccdf48-e518-4ec9-8425-860251fb1dc2",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-29T09:58:27.876844Z",
     "iopub.status.busy": "2022-05-29T09:58:27.876505Z",
     "iopub.status.idle": "2022-05-29T09:58:27.886648Z",
     "shell.execute_reply": "2022-05-29T09:58:27.885670Z",
     "shell.execute_reply.started": "2022-05-29T09:58:27.876819Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>科目</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>学生</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>小红</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>小蓝</th>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "科目  语文  数学  英语\n",
       "学生            \n",
       "小红   0   1   2\n",
       "小蓝   3   4   5"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Series(层级索引)转DataFrame\n",
    "# 指定操作外层索引，用层级名\n",
    "stackeData.unstack(\"科目\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2bbdb9db-de33-4df4-a7e3-1d86b61936e2",
   "metadata": {},
   "source": [
    "### 1.3 不规则层级结构的Series对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "168f5e85-a574-4893-bd57-74d84dc51bab",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-29T10:01:16.458128Z",
     "iopub.status.busy": "2022-05-29T10:01:16.457263Z",
     "iopub.status.idle": "2022-05-29T10:01:16.464454Z",
     "shell.execute_reply": "2022-05-29T10:01:16.463594Z",
     "shell.execute_reply.started": "2022-05-29T10:01:16.458101Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    0\n",
       "b    1\n",
       "c    2\n",
       "d    3\n",
       "e    4\n",
       "dtype: int64"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建实验数据\n",
    "s1 = pd.Series(np.arange(5), index=list(\"abcde\"))\n",
    "s1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "e42584a6-cbab-4511-acde-1cd3beb6b8a8",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-29T10:01:44.579236Z",
     "iopub.status.busy": "2022-05-29T10:01:44.578667Z",
     "iopub.status.idle": "2022-05-29T10:01:44.586350Z",
     "shell.execute_reply": "2022-05-29T10:01:44.584942Z",
     "shell.execute_reply.started": "2022-05-29T10:01:44.579210Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b    0\n",
       "d    1\n",
       "e    2\n",
       "dtype: int64"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建实验数据\n",
    "s2 = pd.Series(np.arange(3), index=list(\"bde\"))\n",
    "s2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "4c02e459-1750-496d-8e92-4682c1e061ec",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-29T10:03:45.963095Z",
     "iopub.status.busy": "2022-05-29T10:03:45.962762Z",
     "iopub.status.idle": "2022-05-29T10:03:45.972907Z",
     "shell.execute_reply": "2022-05-29T10:03:45.972231Z",
     "shell.execute_reply.started": "2022-05-29T10:03:45.963069Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "series1  a    0\n",
       "         b    1\n",
       "         c    2\n",
       "         d    3\n",
       "         e    4\n",
       "series2  b    0\n",
       "         d    1\n",
       "         e    2\n",
       "dtype: int64"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建实验数据\n",
    "sdata = pd.concat([s1, s2], keys=[\"series1\", \"series2\"])\n",
    "sdata"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "f478276a-5555-4e5e-b48b-eedd8c3832a3",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-29T10:04:08.114016Z",
     "iopub.status.busy": "2022-05-29T10:04:08.113672Z",
     "iopub.status.idle": "2022-05-29T10:04:08.126398Z",
     "shell.execute_reply": "2022-05-29T10:04:08.125626Z",
     "shell.execute_reply.started": "2022-05-29T10:04:08.113991Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "      <th>c</th>\n",
       "      <th>d</th>\n",
       "      <th>e</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>series1</th>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>series2</th>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           a    b    c    d    e\n",
       "series1  0.0  1.0  2.0  3.0  4.0\n",
       "series2  NaN  0.0  NaN  1.0  2.0"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Series(层级索引)转DataFrame, 默认补全nan\n",
    "sdata.unstack()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "e541ea59-7d0d-4cef-8fa0-6e2d3b8fcb79",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-29T10:04:31.649233Z",
     "iopub.status.busy": "2022-05-29T10:04:31.648903Z",
     "iopub.status.idle": "2022-05-29T10:04:31.658800Z",
     "shell.execute_reply": "2022-05-29T10:04:31.657977Z",
     "shell.execute_reply.started": "2022-05-29T10:04:31.649210Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "series1  a    0.0\n",
       "         b    1.0\n",
       "         c    2.0\n",
       "         d    3.0\n",
       "         e    4.0\n",
       "series2  b    0.0\n",
       "         d    1.0\n",
       "         e    2.0\n",
       "dtype: float64"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# DataFrame转Series(层级索引)，默认删除nan\n",
    "sdata.unstack().stack()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "186651d2-a51b-459a-959b-e894ae5586df",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-29T10:04:58.456933Z",
     "iopub.status.busy": "2022-05-29T10:04:58.456595Z",
     "iopub.status.idle": "2022-05-29T10:04:58.466513Z",
     "shell.execute_reply": "2022-05-29T10:04:58.465303Z",
     "shell.execute_reply.started": "2022-05-29T10:04:58.456909Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "series1  a    0.0\n",
       "         b    1.0\n",
       "         c    2.0\n",
       "         d    3.0\n",
       "         e    4.0\n",
       "series2  a    NaN\n",
       "         b    0.0\n",
       "         c    NaN\n",
       "         d    1.0\n",
       "         e    2.0\n",
       "dtype: float64"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# DataFrame转Series(层级索引)，设定保留nan\n",
    "sdata.unstack().stack(dropna=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6f19b687-f41a-4742-a51e-dc533393d885",
   "metadata": {},
   "source": [
    "## 二、轴向旋转"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "314db092-e5d2-4684-8e00-65b770d04f9b",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-29T11:13:37.391167Z",
     "iopub.status.busy": "2022-05-29T11:13:37.390691Z",
     "iopub.status.idle": "2022-05-29T11:13:37.401603Z",
     "shell.execute_reply": "2022-05-29T11:13:37.400019Z",
     "shell.execute_reply.started": "2022-05-29T11:13:37.391141Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>date</th>\n",
       "      <th>type</th>\n",
       "      <th>stock</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2022-05-22</td>\n",
       "      <td>手机</td>\n",
       "      <td>123</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2022-05-23</td>\n",
       "      <td>电脑</td>\n",
       "      <td>234</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2022-05-23</td>\n",
       "      <td>配件</td>\n",
       "      <td>543</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2022-05-24</td>\n",
       "      <td>厨具</td>\n",
       "      <td>423</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2022-05-24</td>\n",
       "      <td>食物</td>\n",
       "      <td>543</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         date type  stock\n",
       "0  2022-05-22   手机    123\n",
       "1  2022-05-23   电脑    234\n",
       "2  2022-05-23   配件    543\n",
       "3  2022-05-24   厨具    423\n",
       "4  2022-05-24   食物    543"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建实验数据\n",
    "data2 = pd.DataFrame(\n",
    "    {\n",
    "        \"date\": [\"2022-05-22\", \"2022-05-23\", \"2022-05-23\", \"2022-05-24\", \"2022-05-24\"],\n",
    "        \"type\": [\"手机\", \"电脑\", \"配件\", \"厨具\", \"食物\"],\n",
    "        \"stock\": [123, 234, 543, 423, 543],\n",
    "    }\n",
    ")\n",
    "data2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "e828e9f5-b4c1-4a48-a3fb-9358fe76f0a2",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-29T11:13:45.925686Z",
     "iopub.status.busy": "2022-05-29T11:13:45.925115Z",
     "iopub.status.idle": "2022-05-29T11:13:45.941466Z",
     "shell.execute_reply": "2022-05-29T11:13:45.940436Z",
     "shell.execute_reply.started": "2022-05-29T11:13:45.925660Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th colspan=\"5\" halign=\"left\">stock</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>type</th>\n",
       "      <th>厨具</th>\n",
       "      <th>手机</th>\n",
       "      <th>电脑</th>\n",
       "      <th>配件</th>\n",
       "      <th>食物</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2022-05-22</th>\n",
       "      <td>NaN</td>\n",
       "      <td>123.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-05-23</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>234.0</td>\n",
       "      <td>543.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-05-24</th>\n",
       "      <td>423.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>543.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            stock                            \n",
       "type           厨具     手机     电脑     配件     食物\n",
       "date                                         \n",
       "2022-05-22    NaN  123.0    NaN    NaN    NaN\n",
       "2022-05-23    NaN    NaN  234.0  543.0    NaN\n",
       "2022-05-24  423.0    NaN    NaN    NaN  543.0"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 轴向旋转，set_index和unstack组合实现\n",
    "data2.set_index([\"date\", \"type\"]).unstack(\"type\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "4a5026fd-b7fb-4ce3-af53-472c948021e9",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-29T11:15:18.792611Z",
     "iopub.status.busy": "2022-05-29T11:15:18.792264Z",
     "iopub.status.idle": "2022-05-29T11:15:18.807320Z",
     "shell.execute_reply": "2022-05-29T11:15:18.806403Z",
     "shell.execute_reply.started": "2022-05-29T11:15:18.792587Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th colspan=\"5\" halign=\"left\">stock</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>type</th>\n",
       "      <th>厨具</th>\n",
       "      <th>手机</th>\n",
       "      <th>电脑</th>\n",
       "      <th>配件</th>\n",
       "      <th>食物</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2022-05-22</th>\n",
       "      <td>NaN</td>\n",
       "      <td>123.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-05-23</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>234.0</td>\n",
       "      <td>543.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-05-24</th>\n",
       "      <td>423.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>543.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            stock                            \n",
       "type           厨具     手机     电脑     配件     食物\n",
       "date                                         \n",
       "2022-05-22    NaN  123.0    NaN    NaN    NaN\n",
       "2022-05-23    NaN    NaN  234.0  543.0    NaN\n",
       "2022-05-24  423.0    NaN    NaN    NaN  543.0"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 轴向旋转，pivot组合实现\n",
    "data2.pivot(\"date\", \"type\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "ee509fe8-2119-4dac-a65c-fdbd80d28e04",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-29T11:15:55.819810Z",
     "iopub.status.busy": "2022-05-29T11:15:55.819186Z",
     "iopub.status.idle": "2022-05-29T11:15:55.833893Z",
     "shell.execute_reply": "2022-05-29T11:15:55.832955Z",
     "shell.execute_reply.started": "2022-05-29T11:15:55.819784Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>type</th>\n",
       "      <th>厨具</th>\n",
       "      <th>手机</th>\n",
       "      <th>电脑</th>\n",
       "      <th>配件</th>\n",
       "      <th>食物</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2022-05-22</th>\n",
       "      <td>NaN</td>\n",
       "      <td>123.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-05-23</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>234.0</td>\n",
       "      <td>543.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-05-24</th>\n",
       "      <td>423.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>543.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "type           厨具     手机     电脑     配件     食物\n",
       "date                                         \n",
       "2022-05-22    NaN  123.0    NaN    NaN    NaN\n",
       "2022-05-23    NaN    NaN  234.0  543.0    NaN\n",
       "2022-05-24  423.0    NaN    NaN    NaN  543.0"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 轴向旋转，pivot组合实现\n",
    "data2.pivot(\"date\", \"type\", \"stock\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
